from app import db
from datetime import datetime

class File(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    content = db.Column(db.LargeBinary, nullable=False, info={'mysql_blob_type': 'LONGBLOB'})  # 使用MySQL的LONGBLOB类型(最大4GB)
    file_type = db.Column(db.String(255), nullable=False)  # 增加长度以容纳较长的MIME类型
    file_size = db.Column(db.Integer, nullable=False)
    department_id = db.Column(db.Integer, db.ForeignKey('department.id'), nullable=True)
    folder_name = db.Column(db.String(255), nullable=True)  # 文件夹名称字段
    created_at = db.Column(db.DateTime, default=datetime.now)

    def __repr__(self):
        return f'<File {self.name}>'

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'type': self.file_type,  # 将file_type重命名为type以匹配前端期望
            'size': self.file_size,
            'department_id': self.department_id,
            'folder_name': self.folder_name,
            'uploadTime': self.created_at.isoformat() if self.created_at else None
        }